/* 
 * @project:SOSP
 * @filename:SOSPOAAssignment.java
 */
package com.wolfpire.workflow.assign;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jbpm.api.model.OpenExecution;
import org.jbpm.api.task.Assignable;
import org.jbpm.api.task.AssignmentHandler;

import com.wolfpire.workflow.dao.NodeActorDao;
import com.wolfpire.workflow.dao.NodeDao;
import com.wolfpire.workflow.dao.ProInstanceDao;
import com.wolfpire.workflow.em.ActorType;
import com.wolfpire.workflow.om.Node;
import com.wolfpire.workflow.om.NodeActor;
import com.wolfpire.workflow.om.ProInstance;
import com.wolfpire.workflow.spring.impl.JbpmTaskService;



@SuppressWarnings("serial")
public class DefaultAssignerImpl implements AssignmentHandler {
	
	@Resource private NodeDao nodeDao;
	
	@Resource private ProInstanceDao proInstanceDao;
	
	@Resource private NodeActorDao nodeActorDao;
	
	@Resource private JbpmTaskService jbpmTaskService;

	
	
	public void assign(Assignable assignable, OpenExecution execution) throws Exception {


		
		Long deploymentId  = (Long) execution.getVariable("deploymentId");
		String userIds = (String) execution.getVariable("userIds");

		Node node = nodeDao.getNode(deploymentId, execution.getActivity().getName());
		String nodeName = node.getNodeName();

		// 获取流程类型，判断是否为需求流程，标记阶段状态
//		String formTypeId = (String) execution.getVariable("formTypeId");
		
		//是否给任务发起人审批
		String turnToPromoter = node.getTurnToPromoter();
		if (turnToPromoter!=null && turnToPromoter.equals("1")) {
			String pId = execution.getProcessInstance().getId();
			ProInstance proInstance = null;
			if ( pId != null && !pId.equals("") ) {
				String instanceId = pId.substring(pId.indexOf(".")+1);
				proInstance= proInstanceDao.getProByInstanceId(Long.parseLong(instanceId));
				userIds = String.valueOf(proInstance.getUserId());
			}
		}
		
		if(userIds != null && !"".equals(userIds)){
			String[] userId = userIds.split(",");
			for (String uId : userId) {
				assignable.addParticipation(uId, null, ActorType.USER.getIndex());
			}
			execution.setVariable("userIds", null);
			editProInstance(execution, userIds, null, nodeName);
		} else {
			// System.out.println("正常分配:=======");
			List<NodeActor> actors = nodeActorDao.queryNodeActors(node.getNodeId());
			if (actors != null) {
				for (NodeActor actor : actors) {
					assignable.addParticipation(String.valueOf(actor.getActorId()), null,actor.getActorType());
				}
				editProInstance1(execution, actors, nodeName);
			} else {
				assignable.addParticipation("1", null, "1");
			}
		}
	
	}
	
	private void editProInstance(OpenExecution execution,String userIds,String orgIds,String nodeName){
		
		String pId = execution.getProcessInstance().getId();
		if ( pId != null && !pId.equals("") ) {
			String instanceId = pId.substring(pId.indexOf(".")+1);
			ProInstance proInstance = proInstanceDao.get(Long.parseLong(instanceId));
			if(proInstance!=null){
				
				Map<String, Object> map = jbpmTaskService.getUserOrgName(userIds, orgIds);
				
				proInstance.setCurUser((String) map.get("userNames"));
				proInstance.setCurOrg((String) map.get("orgNames"));
				proInstance.setUpdateTime(new Date());
				proInstance.setCurNode(nodeName);
				proInstanceDao.save(proInstance);
			}
		}
	}
	
	private void editProInstance1(OpenExecution execution,List<NodeActor> actors,String nodeName){
		
		String pId = execution.getProcessInstance().getId();
		if ( pId != null && !pId.equals("") ) {
			String instanceId = pId.substring(pId.indexOf(".")+1);
			ProInstance proInstance = proInstanceDao.get(Long.parseLong(instanceId));
			if(proInstance!=null){
				
				Map<String, Object> map = jbpmTaskService.getUserOrgName(actors);
				
				proInstance.setCurUser((String) map.get("userNames"));
				proInstance.setCurOrg((String) map.get("orgNames"));
				proInstance.setUpdateTime(new Date());
				proInstance.setCurNode(nodeName);
				proInstanceDao.save(proInstance);
			}
		}
	}
}
